package com.nbsaas.boot.shopping.ext.resource;

import com.nbsaas.boot.product.api.apis.ProductApi;
import com.nbsaas.boot.product.api.apis.ProductSkuApi;
import com.nbsaas.boot.product.api.domain.response.ProductResponse;
import com.nbsaas.boot.product.api.domain.response.ProductSkuResponse;
import com.nbsaas.boot.rest.filter.Filter;
import com.nbsaas.boot.rest.response.ResponseObject;
import com.nbsaas.boot.shopping.api.apis.ShoppingCartApi;
import com.nbsaas.boot.shopping.api.domain.field.ShoppingCartField;
import com.nbsaas.boot.shopping.api.domain.request.ShoppingCartRequest;
import com.nbsaas.boot.shopping.api.domain.response.ShoppingCartResponse;
import com.nbsaas.boot.shopping.ext.apis.ShoppingCartExtApi;
import com.nbsaas.boot.shopping.ext.domain.request.ShoppingCartCheckedRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Service
public class ShoppingCartExtResource implements ShoppingCartExtApi {

    @Resource
    private ShoppingCartApi shoppingCartApi;

    @Resource
    private ProductApi productApi;

    @Resource
    private ProductSkuApi productSkuApi;

    @Transactional
    @Override
    public ResponseObject<?> checked(ShoppingCartCheckedRequest request) {
        ResponseObject<?> result = new ResponseObject<>();
        for (Long id : request.getSkuList()) {
            ShoppingCartRequest cartRequest = new ShoppingCartRequest();
            cartRequest.setId(id);
            cartRequest.setChecked(request.getChecked());
            shoppingCartApi.updateData(cartRequest);
        }
        return result;
    }

    @Transactional
    @Override
    public ResponseObject<?> createExt(ShoppingCartRequest request) {
        ResponseObject<?> result = new ResponseObject<>();
        request.setChecked(true);
        if (request.getProduct() == null) {
            result.setCode(500);
            result.setMsg("商品不能为空");
            return result;
        }
        if (request.getNum()==null){
            result.setCode(500);
            result.setMsg("商品数量不能为空");
            return result;
        }
        ProductResponse product = productApi.oneData(Filter.eq("id", request.getProduct()));
        if (product == null) {
            result.setCode(500);
            result.setMsg("商品不存在");
            return result;
        }
        ShoppingCartResponse cartResponse = null;
        if (product.getSkuEnable()) {
            if (request.getSku() == null) {
                result.setCode(500);
                result.setMsg("商品规格不能为空");
                return result;
            }
            ProductSkuResponse sku = productSkuApi.oneData(Filter.eq("id", request.getSku()));
            if (sku == null) {
                result.setCode(500);
                result.setMsg("商品规格不存在");
                return result;
            }
            request.setPrice(sku.getPrice());

            //判断购物车是否存在该商品
            cartResponse = shoppingCartApi.oneData(Filter.eq(ShoppingCartField.product, request.getProduct()),
                    Filter.eq(ShoppingCartField.creator, request.getCreator()),
                    Filter.eq(ShoppingCartField.sku, request.getSku())

            );
        } else {
            request.setPrice(product.getPrice());
            //判断购物车是否存在该商品
            cartResponse = shoppingCartApi.oneData(Filter.eq(ShoppingCartField.product, request.getProduct()),
                    Filter.eq(ShoppingCartField.creator, request.getCreator())
            );
        }

        if (cartResponse==null){
            if (request.getPrice()==null){
                request.setPrice(BigDecimal.ZERO);
            }
            request.setTotalPrice(request.getPrice().multiply(new BigDecimal(request.getNum())));
            shoppingCartApi.createData(request);
        }else{
            request.setId(cartResponse.getId());
            Integer num = cartResponse.getNum();
            if (num == null){
                num = 0;
            }
            request.setNum(num + request.getNum());
            if (request.getPrice()==null){
                request.setPrice(BigDecimal.ZERO);
            }
            request.setTotalPrice(request.getPrice().multiply(new BigDecimal(request.getNum())));
            shoppingCartApi.updateData(request);
        }
        return result;
    }
}
